package android.pot.com.smartcar.tool;

import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhaoshilu on 2018/1/26.
 */
public class ReadFile {

    public static Map<String,String> getFileList(){
        Map<String,String> fileNameMap = new HashMap<>();
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/crashs";
            File dir = new File(path);
            if (dir.exists()){
                File[] fils = dir.listFiles();
                for (File files:fils){
                    String contents = readFile(files.getPath());
                    fileNameMap.put(files.getName(),contents);
                }
            }
        }
        return fileNameMap;
    }

    private static String readFile(String path){
        FileInputStream inputStream;
        StringBuilder sb =  new StringBuilder("");
        try {
            inputStream = new FileInputStream(path);
            byte temp[] = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(temp)) > 0){
                sb.append(new String(temp, 0, len));
            }
            inputStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
}